package com.yuan.model_convenient.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_convenient.domain.ConvenientActivity;
import com.yuan.model_convenient.repository.ConvenientActivityDao;
import com.yuan.model_convenient.service.ConvenientActivityService;
import com.yuan.model_convenient.service.dto.ConvenientActivityDto;
import com.yuan.model_convenient.service.dto.ConvenientActivityQueryCriteria;
import com.yuan.model_convenient.service.mapstruct.ConvenientActivityMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class ConvenientActivityServiceImpl implements ConvenientActivityService {

    private final ConvenientActivityDao convenientActivityDao;

    private final ConvenientActivityMapper convenientActivityMapper;

    @Override
    public void download(List<ConvenientActivityDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ConvenientActivityDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<ConvenientActivityDto> queryAll(Pageable pageable) {
        Page<ConvenientActivity> all = convenientActivityDao.findAll(pageable);
        return PageUtil.toPage(all.map(convenientActivityMapper::toDto));
    }

    @Override
    public PageResult<ConvenientActivityDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<ConvenientActivity> page = convenientActivityDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(convenientActivityMapper::toDto));
    }

    @Override
    public PageResult<ConvenientActivityDto> queryAllByType(ConvenientActivityQueryCriteria criteria, Pageable pageable) {
        Page<ConvenientActivity> page = convenientActivityDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(convenientActivityMapper::toDto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConvenientActivity resources) {
        convenientActivityDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonNum(Integer activityId){
        Optional<ConvenientActivity> byId = convenientActivityDao.findById(activityId);
        if (byId.isPresent()) {
            ConvenientActivity convenientActivity = byId.get();
            convenientActivity.setActivityActualPerson(convenientActivity.getActivityNeedPerson() -1 );
            convenientActivityDao.save(convenientActivity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConvenientActivity resources) throws Exception {
        Optional<ConvenientActivity> byId = convenientActivityDao.findById(resources.getActivityId());
        if (byId.isPresent()) {
            ConvenientActivity convenientActivity = byId.get();
            BeanUtils.copyProperties(resources, convenientActivity);
            convenientActivityDao.save(convenientActivity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        convenientActivityDao.deleteById(ids);
    }
}
